package com.hq.ims.data.service;

import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.dao.BusInfoFeedbackMapper;
import com.hq.ims.data.entity.BusInfoFeedback;
import com.hq.ims.data.entity.BusInfoTrack;
import com.hq.ims.data.entity.FileRecord;
import com.hq.ims.data.utils.LocalConstant;
import com.hq.ims.data.vo.AppFileMapstruct;
import com.hq.ims.data.vo.BusInfoFeedbackMapstruct;
import com.hq.ims.data.vo.BusiIndustryAssignMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.utils.util.ListUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 蒋柏平
 * @detail 经营信息反馈 服务实现类
 * @since 20241024
 */
@Service
public class BaseBusInfoFeedbackService extends BaseService<BusInfoFeedbackMapper, BusInfoFeedback> {

    @Resource
    private BaseBusInfoTrackService baseBusInfoTrackService;
    @Resource
    private BaseBusInfoFeedbackDeriveService baseBusInfoFeedbackDeriveService;
    @Resource
    private BaseFileRecordService baseFileRecordService;

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public BusInfoFeedback addData(BusInfoFeedbackMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);

        BusInfoTrack busInfoTrack = baseBusInfoTrackService.getById(vo.getBusInfoTrackId());
        //账值新的对象
        BusInfoFeedback entity = new BusInfoFeedback();
        entity.setBusInfoTrackId(busInfoTrack.getBusInfoTrackId());
        entity.setIndustryMarkId(busInfoTrack.getIndustryMarkId());
        entity.setIndustryMarkName(busInfoTrack.getIndustryMarkName());
        entity.setFeedbackPhase(vo.getFeedbackPhase());
        entity.setFeedbackDate(LocalDateTime.now());
        entity.setContent(vo.getContent());
        entity.setBusiPersonId(busInfoTrack.getBusiPersonId());
        entity.setBusiPersonName(busInfoTrack.getBusiPersonName());
        entity.setFeedbackFileFlag(ListUtils.isExistsEle(vo.getFileVoList()));
        if (CollectionUtils.isNotEmpty(vo.getDeriveBusiInfoList())) {
            entity.setDeriveBusiInfoCount(vo.getDeriveBusiInfoList().size());
        }
        entity.setBusiInfoId(busInfoTrack.getBusiInfoId());
        this.save(entity);
        return entity;
    }

    /**
     * @description: 拼接反馈情况列表
     * @author: yinzhangxiao
     * @date: 2024/11/17 16:28
     */
    public void montageFeedBack(BusiIndustryAssignMapstruct.BusiIndustryAssignVo busiIndustryAssignVo) {
        // 反馈情况列表
        List<BusInfoFeedback> busInfoFeedbackList = this.lambdaQuery()
                .eq(BusInfoFeedback::getIndustryMarkId, busiIndustryAssignVo.getIndustryId())
                .eq(BusInfoFeedback::getBusiInfoId, busiIndustryAssignVo.getBusiInfoId())
                .orderByDesc(BusInfoFeedback::getFeedbackDate)
                .list();
        if (CollectionUtils.isNotEmpty(busInfoFeedbackList)) {
            List<BusInfoFeedbackMapstruct.FeedbackDetailVo> feedbackDetailVoList = assembleFeedbackFileAndDeriveInfo(busInfoFeedbackList);
            busiIndustryAssignVo.setFeedbackVoList(feedbackDetailVoList);
        }
    }

    /**
     * 组装反馈文件和派生信息数据
     *
     * @param busInfoFeedbackList 反馈列表
     * @return List<BusInfoFeedbackMapstruct.FeedbackDetailVo>
     */
    public List<BusInfoFeedbackMapstruct.FeedbackDetailVo> assembleFeedbackFileAndDeriveInfo(List<BusInfoFeedback> busInfoFeedbackList) {
        List<BusInfoFeedbackMapstruct.FeedbackDetailVo> feedbackDetailVoList = BusInfoFeedbackMapstruct.INSTANCE.toFeedbackDetailVoList(busInfoFeedbackList);
        feedbackDetailVoList.forEach(busInfoFeedbackVo -> {
            if (busInfoFeedbackVo.getFeedbackFileFlag()) {
                // 反馈内容文件
                List<FileRecord> fileRecordList = baseFileRecordService.lambdaQuery()
                        .eq(FileRecord::getFileMark, LocalConstant.FILE_FEEDBACK_CONTENT)
                        .eq(FileRecord::getOwnTableDataId, busInfoFeedbackVo.getBusInfoFeedbackId())
                        .list();
                List<AppFileMapstruct.FileVo> fileVoList = baseFileRecordService.changeFileRecordToFileVo(fileRecordList);
                busInfoFeedbackVo.setFileVoList(fileVoList);
            }
            // 派生的经营信息数量
            Integer deriveBusiInfoCount = busInfoFeedbackVo.getDeriveBusiInfoCount();
            if (deriveBusiInfoCount > 0) {
                // 查询派生的经营信息列表
                List<BusInfoFeedbackMapstruct.DeriveBusiInfoVo> deriveBusiInfoList =
                        baseBusInfoFeedbackDeriveService.getDeriveBusiInfoList(busInfoFeedbackVo.getBusInfoFeedbackId());
                busInfoFeedbackVo.setDeriveBusiInfoList(deriveBusiInfoList);
            }
        });
        return feedbackDetailVoList;
    }

}